<template>
  <div>
  <el-table :data="tableData">
    <el-table-column prop="id" label="ID"></el-table-column>
    <el-table-column prop="username" label="用户名"></el-table-column>
    <el-table-column prop="phone" label="电话"></el-table-column>
    <el-table-column prop="email" label="邮箱"></el-table-column>
    <el-table-column prop="status" label="状态"></el-table-column>
    <el-table-column label="操作">
      <template slot-scope="scope">
        <el-button type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>
        <el-button type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
      </template>
    </el-table-column>
    
    <el-button type="primary" icon="el-icon-plus" @click="showAddDialog">增加新用户</el-button>
  </el-table>
  <el-dialog :visible.sync="dialogVisible" title="增加用户信息">
      <el-form :model="formData"  ref="form">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="formData.username"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="formData.phone"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="formData.email"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="formData.status">
            <el-radio :label="1" value="active">启用</el-radio>
            <el-radio :label="0" value="active">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="submitForm">保存</el-button>
      </div>
    </el-dialog>
    <el-button type="primary" icon="el-icon-plus" @click="showAddDialog">新增</el-button>
  </div>
</template>

<script>


export default {
  data() {
    return {
      tableData: [
        { id: 1, username: '张三', phone: '13812345678', email: 'zhangsan@xxx.com', status: '启用' },
      ],
      currentPerson: {
        id: '',
        username: '',
        phone: '',
        email: '',
        status: 'active'
      },
      dialogVisible: false,
      formData: {},
      
    }
  },
  mounted(){
    this.fetchTableData();
  },

  methods: {
    async fetchPeopleList() {
      try {
        const response = await getPeopleList(this.token)
        this.peopleList = response.data
      } catch (error) {
        console.log(error)
      }
    },
    showAddDialog() {
      this.currentPerson = {
        id: '',
        username: '',
        phone: '',
        email: '',
        status: 'active'
      };
     this.dialogVisible = true;
 
    },
    async submitForm() {
      if (this.currentPerson.id) {
        try {
          await updatePerson(this.token, this.currentPerson.id, this.currentPerson)
          this.dialogVisible = false
          this.fetchPeopleList()
        } catch (error) {
          console.log(error)
        }
      } else {
        try {
          await addPerson(this.token, this.currentPerson)
          this.dialogVisible = false
          this.fetchPeopleList()
        } catch (error) {
          console.log(error)
        }
      }
    },
    handleEdit(row) {
      
      this.formData = { ...row };
      
      this.dialogVisible = true;

    },
    handleDelete(row) {
      // 删除操作
      this.formData = { ...row };
      row={};
    },
    cancel(){
      this.dialogVisible = false;
    }
  }
}
</script>